/datum/file/program/notepad
	name					= "Notekeeper"
	image					= 'icons/NTOS/program.png'
	desc					= "A program that reads and writes to text files. Needed for reading or writing text files."
	active_state			= "generic"
/*
	A generic file that contains text
*/

/datum/file/data/text //A Bit of a copy/paste from modules/paperwork/paper.dm
	name = "Text File"
	extension = "txt"
	image = 'icons/NTOS/file.png'
	content = ""
	var/info_links	//A different version of the paper which includes html links at fields and EOF
	var/fields		//Amount of user created fields
	file_increment = 0.002 // 0.002 kilobytes per character (1024 characters per KB)

/datum/file/data/text/proc/addtofield(id, text, links = 0)
	var/locid = 0
	var/laststart = 1
	var/textindex = 1
	while(1)	//I know this can cause infinite loops and fuck up the whole server, but the if(istart==0) should be safe as fuck
		var/istart = 0
		if(links)
			istart = findtext(info_links, "<span class=\"paper_field\">", laststart)
		else
			istart = findtext(content, "<span class=\"paper_field\">", laststart)

		if(istart == 0)
			return	//No field found with matching id

		laststart = istart+1
		locid++
		if(locid == id)
			var/iend = 1
			if(links)
				iend = findtext(info_links, "</span>", istart)
			else
				iend = findtext(content, "</span>", istart)

			//textindex = istart+26
			textindex = iend
			break

	if(links)
		var/before = copytext(info_links, 1, textindex)
		var/after = copytext(info_links, textindex)
		info_links = before + text + after
	else
		var/before = copytext(content, 1, textindex)
		var/after = copytext(content, textindex)
		content = before + text + after
		updateinfolinks()

/datum/file/data/text/proc/updateinfolinks()
	info_links = content
	var/i = 0
	for(i=1,i<=fields,i++)
		addtofield(i, "<A href='?src=\ref[src];write=[i]'>type</A></font>", 1)
	info_links = info_links + "<A href='?src=\ref[src];write=end'>type</A></font>"

/datum/file/data/text/proc/clearall()
	content = null

/datum/file/data/text/proc/parsecode()
		replacetext(content, "\[center\]", "<center>")
		replacetext(content, "\[/center\]", "</center>")
		replacetext(content, "\[br\]", "<BR>")
		replacetext(content, "\[b\]", "<B>")
		replacetext(content, "\[/b\]", "</B>")
		replacetext(content, "\[i\]", "<I>")
		replacetext(content, "\[/i\]", "</I>")
		replacetext(content, "\[u\]", "<U>")
		replacetext(content, "\[/u\]", "</U>")
		replacetext(content, "\[large\]", "<font size=\"4\">")
		replacetext(content, "\[/large\]", "</font>")
		replacetext(content, "\[field\]", "<span class=\"paper_field\"></span>")
		return

/datum/file/data/text/proc/rename()
	var/n_name = copytext(sanitize(input(usr, "What would you like to name the file?", "Text File Labelling", null)  as text), 1, MAX_NAME_LEN)
		name = "[(n_name ? text("[n_name]") : null)]"

/datum/file/data/text/interact()
	var/dat// = topic_link(src,"close","Close")
	dat += "[topic_link(src,"clear","Clear all data")] | [topic_link(src,"rename","Rename File")]"
	dat += "<center><h4>[name]</h4></center><br>"
	dat += "[content]"

	popup.set_content(dat)
	popup.open()

/datum/file/data/text/Topic(href, list/href_list)
	if(!interactable() || ..(href,href_list))
		return
		if ("clear" in href_list)
			clearall()
		else if ("rename" in href_list)
			rename()

/datum/file/data/text/ClownProphecy
	name = "Clown Prophecy"
	content = "HONKhHONKeHONKlHONKpHONKHONmKHONKeHONKHONKpHONKlHONKeHONKaHONKsHONKe"

/*
	Now for the actual program bits (huehuehue)
*/

/datum/file/program/notepad